﻿#include "Player.h"
#include <math.h>
#define CONTRA_VX 2.2f 
#define TIME 1000.0f / 60.0f 
#define R_BULLET R
#define V_JUMP 8.5f
#include "ShareComponents.h"
#include"RSManager.h"
#include "Sprite.h"
#include "FWKeyboard.h"
#include "Camera.h"
#include "Box.h"
#include "PlayerState.h"
#include "OnSpaceState.h"
#include "OnGroundState.h"
#include "OnWaterState.h"
#include "OnDieState.h"
#include "BulletManager.h"
#include "StaticObject.h"

Player::Player(Box* box): Object(box, ObjPlayer), MoveObject()
{	
	_bulletID = B_R;	
}

Player::~Player()
{

}
void Player::init()
{
	this->delay = 0;
	_isALive = true;
	_onGroundState = new OnGroundState(this);
	_onSpaceState = new OnSpaceState(this);
	_onWaterState = new OnWaterState(this);
	_onDieState = new OnDieState(this);

	_state = _onSpaceState;
	_enumState = ONSPACE;
	_preState = ONSPACE;

	this->AssignMove(this->_box);
	gl_player = _box;
}
void Player::update()
{
	if(_isALive)
	{
		gl_player = _box;
		gl_ground_player = _getBoundBox();
		gl_move = _getMoveBox();
		//Next frame
		this->delay += TIME;
		if (delay > 85)
		{
			delay = 0;				
			_sprite->Next();
		}

		switch (_enumState)
		{
		case ONGROUND:
			gl_ground = _getGround();
			_state = _onGroundState;
			if(_preState == ONWATER)
				_state->setDirection(_onWaterState->getJumpDirection(), _onWaterState->getGoDirection());
			if(_preState == ONSPACE)
				_state->setDirection(_onSpaceState->getJumpDirection(), _onSpaceState->getGoDirection());
			_preState = ONGROUND;
			if(_getCurrentGround() == NULL)
			{
				_onGroundState->setJumpDirection(DOWN);
				_state = _onSpaceState;
			}
			else
				if(_getCurrentGround()->getID() == WATER)
					_state = _onWaterState;
			break;
		case ONSPACE:
			_state = _onSpaceState;
			if(_preState == ONGROUND)
			{			
				_onSpaceState->jump(_onGroundState->getJumpDirection(), _onGroundState->getGoDirection());			
			}
			_preState = ONSPACE;
			if(isOnGround() && _box->_v.y < 0)
			{
				_state->setDirection(_onSpaceState->getJumpDirection(), _onSpaceState->getGoDirection());
				_state = _onGroundState;
			}		
			if(isOnWater())
				_state = _onWaterState;
			break;
		case ONWATER:
			_state = _onDieState;
			jumpUp();		
			break;
		case ONDIE:
			_state = _onDieState;
			if(_sprite->GetCurrentIndex() == 4)
				_isALive = false;
			// 		_state = _onWaterState;
			// 		if(_preState == ONGROUND)
			// 			_state->setDirection(_onGroundState->getJumpDirection(), _onGroundState->getGoDirection());
			// 		if(_preState == ONSPACE)
			// 			_state->setDirection(_onSpaceState->getJumpDirection(), _onSpaceState->getGoDirection());
			// 		_preState = ONWATER;
			// 		if(!isOnWater())
			// 			_state = _onGroundState;
			break;
		default:
			break;
		}		

		_enumState = _state->handleInput();

		setPlayerBox();

		this->move();	
	}
}
void Player::render()
{			
	_sprite->Render(_box->_position);
}


void Player::setPlayerBox()
{
	_box->_size = _sprite->getRenderSize();	
	//_box->_size.y /= 2;	
}

Box* Player::getPlayerBox()
{
	return _box;
}

void Player::setSprite(Sprite* s)
{
	_sprite = s;
}

void Player::jumpUp()
{
	_box->_v.y = V_JUMP;
}

void Player::jumpDown()
{
	if(canFallDown())
		this->fallDown();
}

void Player::set_vx(float vx)
{
	if(vx < 0)
		if(_box->_position.x > gl_camera->getLeftSide() + 5)
			_box->_v.x = vx;
		else
			_box->_v.x = 0;
	else
		if(_box->_position.x <= gl_camera->getRightSide() - 5)
			_box->_v.x = vx;
		else
			_box->_v.x = 0;
}

void Player::setState(PlayerState* state)
{
	_state = state;
}

void Player::setBulletID(BULLET_ID b)
{
	_bulletID = b;
}

BULLET_ID Player::getBulletID()
{
	return _bulletID;
}

void Player::shoot(int pos_x, int pos_y, float angle)
{
	BulletManager::getInstance()->shoot(_bulletID, _obj, _box->_position + D3DXVECTOR2(pos_x, pos_y), angle);
}
